var file1 = '../dingdan1/dingdan'
var file2 = '../dingdan2/dingdan'
var file3 = '../dingdan3/dingdan'
var file4 = '../dingdan4/dingdan'
var file5 = '../dingdan5/dingdan'
var file6 = '../dingdan6/dingdan'
var file7 = '../dingdan7/dingdan'
var file8 = '../dingdan8/dingdan'
var file9 = '../dingdan9/dingdan'
var app = getApp()
Page({
  data:{
    
    foodList:[],
    detailFood:{},
    modalHidden: true,

    //订单数据
    orderList: {},
    orderNum: 0,
    orderCost: 0,
    toastHidden: true,

    //添加食品列表
    goods: [], 

    //轮播图
    imgUrls: [
      {
         id: '1',
         src: 'http://oky0avxwu.bkt.clouddn.com/food-01.jpg',
      },
      {
         id: '2',
         src: 'http://oky0avxwu.bkt.clouddn.com/food-02.jpg',
      },
      {
         id: '3',
         src: 'http://oky0avxwu.bkt.clouddn.com/food-03.jpg',
      },
      {
         id: '4',
         src: 'http://oky0avxwu.bkt.clouddn.com/food-04.jpg',
      },
    ],
    indicatorDots: true,
    autoplay: true,
    interval: 4000,
    duration: 1000,

    //picker
    foodTypes: ['全部菜品', '披萨', '面条', '水果', '寿司', '三明治'],
    foodTypesIndex: 0,
    rankTypes: ['综合排序', '热度', '价格', '好评', '时间'],
    rankTypesIndex: 0
  },
  //跳转食物详情页
  food1: function() {
      wx.navigateTo({
        //传递参数方式向get请求拼接参数一样
        url:  file1 + '?phone=18939571&password=1992',
        success: function(res) {
          console.log(res)
        },
        fail: function(err) {
          console.log(err)
        }

      })
  },
  food2: function() {
      wx.navigateTo({
        //传递参数方式向get请求拼接参数一样
        url:  file2 + '?phone=18939571&password=1992',
        success: function(res) {
          console.log(res)
        },
        fail: function(err) {
          console.log(err)
        }

      })
  },
  food3: function() {
      wx.navigateTo({
        //传递参数方式向get请求拼接参数一样
        url:  file3 + '?phone=18939571&password=1992',
        success: function(res) {
          console.log(res)
        },
        fail: function(err) {
          console.log(err)
        }

      })
  },
  food4: function() {
      wx.navigateTo({
        //传递参数方式向get请求拼接参数一样
        url:  file4 + '?phone=18939571&password=1992',
        success: function(res) {
          console.log(res)
        },
        fail: function(err) {
          console.log(err)
        }

      })
  },
  food5: function() {
      wx.navigateTo({
        //传递参数方式向get请求拼接参数一样
        url:  file5 + '?phone=18939571&password=1992',
        success: function(res) {
          console.log(res)
        },
        fail: function(err) {
          console.log(err)
        }

      })
  },
  food6: function() {
      wx.navigateTo({
        //传递参数方式向get请求拼接参数一样
        url:  file6 + '?phone=18939571&password=1992',
        success: function(res) {
          console.log(res)
        },
        fail: function(err) {
          console.log(err)
        }

      })
  },
  food7: function() {
      wx.navigateTo({
        //传递参数方式向get请求拼接参数一样
        url:  file7 + '?phone=18939571&password=1992',
        success: function(res) {
          console.log(res)
        },
        fail: function(err) {
          console.log(err)
        }

      })
  },
  food8: function() {
      wx.navigateTo({
        //传递参数方式向get请求拼接参数一样
        url:  file8 + '?phone=18939571&password=1992',
        success: function(res) {
          console.log(res)
        },
        fail: function(err) {
          console.log(err)
        }

      })
  },
  food9: function() {
      wx.navigateTo({
        //传递参数方式向get请求拼接参数一样
        url:  file9 + '?phone=18939571&password=1992',
        success: function(res) {
          console.log(res)
        },
        fail: function(err) {
          console.log(err)
        }

      })
  },

  // 页面初始化 options为页面跳转所带来的参数
  onLoad:function(options){

    //全局数据中获得食品列表
    var appInstance = getApp();
    var t_foodList = appInstance.globalData.foodList;

    var t_arr;
    for(var i=0; i<t_foodList.length; i++){
      if(i%2 == 0){
        t_arr = [];
        this.data.foodList.push(t_arr); //整理成二维数据，方便显示
      }
      t_arr.push(t_foodList[i]);
    }

     //动态获取食品数据
     this.set
     var that = this;
     wx.showToast({
        title: '加载中',
        icon: 'Loading',
        duration: 500,
    },
    wx.request ({
      url: 'https://canyin.api.yanxishe.cc/web/api/food/list',
      data: {},
      method: 'GET', 
      header: {"Content-Type":"json"},
      success: function(res){
        wx.hideToast()
        console.log(res);
        that.setData({goods:res.data.data});
        console.log(res.data.data);
      },
      fail: function() {
        console.log("商家不在家!");
      },
      complete: function() {
        console.log("商家正在营业!");
      }
    })
    )

  },

  //关闭食品详情弹窗
  closeModal: function(e) {
    this.setData({
      modalHidden: true
    })
  },

  //展示食品详情弹窗
  imageClick:function(e){

    var dataset = e.currentTarget.dataset;
    
    var appInstance = getApp();
    var t_foodList = appInstance.globalData.foodList;

    //设置数据，自动刷新界面
    this.setData({
      modalHidden: false,
      detailFood: t_foodList[dataset.index],
      detailIndex: dataset.index
    })
  },

  //提交订单
  sublimitClick:function(e){

    //订单列表 传参
    var agrs = JSON.stringify(this.data.orderList);
    wx.navigateTo({
        url: '../order/order?order=' + agrs
    })
  },

  //加入到购物车
  addCartClick:function(e){
    var dataset = e.currentTarget.dataset;
    this.changeNum(dataset.index, true);

    this.setData({
      toastHidden: false
    });

    //1s后关闭
    var _this = this;
    setTimeout(function(){
      _this.setData({
        toastHidden: true
      });
    }, 1000);

    //关闭商品详情面板
    this.closeModal();
  },

  //增加数量
  addClick:function(e){
    var dataset = e.currentTarget.dataset;
    this.changeNum(dataset.index, true);
  },

  //减少数量
  reduceClick:function(e){
    var dataset = e.currentTarget.dataset;
    this.changeNum(dataset.index, false);
  },

  changeNum:function(index, bool){
    var appInstance = getApp();
    var t_food = appInstance.globalData.foodList[index];

    var orderList = this.data.orderList;

    var obj = orderList[t_food.id];

    //如果存在，则数量变化
    if(obj){
        if(bool){
          obj.num = obj.num + 1;
        }else{
          if(obj.num > 0){
            obj.num = obj.num - 1;
          }else{
            return;//已经减少为0
          }
        }
    }else{
        if(bool){
          //不存在，点击增加，则写入一条订单数据，数量默认1
          obj = {
            id: t_food.id,
            num: 1,
            cost: t_food.cost,
            title: t_food.title
          };
          this.data.orderList[t_food.id] = obj;
        }else{
          return;//不存在，并且点击的是减少
        }
    }
    
    var order_num = 0;
    var order_cost = 0;
    for(var k in orderList){
      order_num = orderList[k].num + order_num; //计算总数量
      order_cost = order_cost + orderList[k].cost * orderList[k].num; //计算总价格
    }

    this.setData({
      orderList: orderList,
      orderNum: order_num,
      orderCost: order_cost
    });
  },

  //食品类型
  foodTypeChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      foodTypesIndex: e.detail.value
    })
  },

  //排序类型
  rankTypeChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      rankTypesIndex: e.detail.value
    })
  },

})